package com.hxq.library.controller;

import com.hxq.library.service.IUserService;
import com.hxq.library.service.Impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

/**
 * @Author: Ljx
 * @Date: 2021/11/25 17:13
 * @role:
 */
@WebServlet("/updatePassword")
public class UpdatePasswordServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html,utf-8");
        HttpSession session = req.getSession();
        String account = (String) session.getAttribute("account");
        String oldpassword = req.getParameter("oldpassword");
        String newpassword = req.getParameter("newpassword");
        String newpassword1 = req.getParameter("newpassword1");
        if (oldpassword==""||newpassword==""||newpassword1==""){
            req.setAttribute("msg3","输入的值不能为空！！！");
            req.getRequestDispatcher("updatePassword.jsp").forward(req,resp);
        }else if (!Objects.equals(newpassword, newpassword1)){
            req.setAttribute("msg3","两次输入密码不同！！！");
            req.getRequestDispatcher("updatePassword.jsp").forward(req,resp);
        }else {
            IUserService service = new UserService();
            try {
                boolean changePassword = service.changePassword(account, oldpassword, newpassword);
                session.setAttribute("updatePassword",changePassword);
                resp.sendRedirect(req.getContextPath()+"booleanIpdatePassword.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
